package com.yuhang.demo.controller;

import com.yuhang.demo.dto.request.RuleAddRequest;
import com.yuhang.demo.dto.request.RuleUpdateRequest;
import com.yuhang.demo.dto.response.Result;
import com.yuhang.demo.service.RuleInfoService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rule")
public class RuleInfoController {

    @Autowired
    private RuleInfoService ruleInfoService;

    /**
     * 新增规则
     */
    @PostMapping("/add")
    public Result<Long> addRule(@Valid @RequestBody RuleAddRequest request) {
        return ruleInfoService.addRule(request);
    }

    /**
     * 更新规则
     */
    @PutMapping("/update")
    public Result<Boolean> updateRule(@Valid @RequestBody RuleUpdateRequest request) {
        return ruleInfoService.updateRule(request);
    }

    /**
     * 删除规则
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteRule(@PathVariable Long id) {
        return ruleInfoService.deleteRule(id);
    }

    /**
     * 分页查询规则列表（支持两类规则筛选）
     */
    @GetMapping("/page")
    public Result<?> getRulePage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String ruleType,
            @RequestParam(required = false) String itemName, // 原 drugGenericName 改为 itemName
            @RequestParam(required = false) String limitGender,
            @RequestParam(required = false) String nationalItemCode) { // 新增医疗服务项目代码参数

        // 调用Service时传递新参数
        return ruleInfoService.getRulePage(
                pageNum,
                pageSize,
                ruleType,
                itemName,
                limitGender,
                nationalItemCode
        );
    }

    /**
     * 查询规则详情
     */
    @GetMapping("/detail/{id}")
    public Result<?> getRuleById(@PathVariable Long id) {
        return ruleInfoService.getRuleById(id);
    }
}
